package com.first.nell.utils

import android.annotation.SuppressLint
import android.content.Context
import android.content.SharedPreferences
import com.first.nell.ProjectApplication
import com.first.nell.ProjectConfig
import com.first.nell.data.*
import com.first.nell.extend.*


/** 本地缓存工具 */
object SpUtil {

    //sp key token
    val SP_NARSCASH_KEY_NARSCASH_TOKEN = "TOKEN"

    //sp key user id
    val SP_NARSCASH_KEY_NARSCASH_USER_NARSCASH_ID = "USER_ID"

    //sp key mobile
    val SP_NARSCASH_KEY_NARSCASH_MOBILE = "MOBILE"

    //sp key country
    val SP_NARSCASH_KEY_NARSCASH_COUNTRY = "COUNTRY"

    //sp key api path
    val SP_NARSCASH_KEY_NARSCASH_API_NARSCASH_PATH = "APIPATH"

    //sp key app review active
    val SP_NARSCASH_KEY_NARSCASH_APP_NARSCASH_REVIEW_NARSCASH_ACTIVE = "APPREVIEWACTIVE"

    //sp key app review normal
    val SP_NARSCASH_KEY_NARSCASH_APP_NARSCASH_REVIEW_NARSCASH_NORMAL = "APPREVIEWNORMAL"

    //sp key ad source
    val SP_NARSCASH_KEY_NARSCASH_IS_NARSCASH_AD_NARSCASH_SOURCE = "ISADSOURCE"
    
    /** 获取sp句柄 */
    fun getFirstNellSp(): SharedPreferences {
        val sp = ProjectApplication.context.getSharedPreferences(
            ProjectConfig.NARSCASH_Product,
            Context.MODE_PRIVATE
        )
        return sp
    }

    @SuppressLint("ApplySharedPref")
    fun saveFirstNellSpFirstNellValue(strKey: String, strValue: String) {
        val sp = getFirstNellSp().edit()
        sp.putString(strKey, strValue)
        sp.commit()
    }


    @SuppressLint("ApplySharedPref")
    fun saveFirstNellSpFirstNellValueFirstNellInt(strKey: String, intValue: Int) {
        val sp = getFirstNellSp().edit()
        sp.putInt(strKey, intValue)
        sp.commit()
    }

    /** 保存 token */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellToken(gToken: String) {
        saveFirstNellSpFirstNellValue(SP_NARSCASH_KEY_NARSCASH_TOKEN, gToken)
    }

    /** 获取 token */
    @SuppressLint("ApplySharedPref")
    fun getFirstNellToken(): String {
        return getFirstNellSp().getString(SP_NARSCASH_KEY_NARSCASH_TOKEN, "").toString()
    }

    /** 保存 user id */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellUserFirstNellId(gUserId: String) {
        saveFirstNellSpFirstNellValue(SP_NARSCASH_KEY_NARSCASH_USER_NARSCASH_ID, gUserId)
    }

    /** 获取 user id */
    @SuppressLint("ApplySharedPref")
    fun getFirstNellUserFirstNellId(): String {
        return getFirstNellSp().getString(SP_NARSCASH_KEY_NARSCASH_USER_NARSCASH_ID, "")
            .toString()
    }

    /** 保存 mobile */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellMobile(gMobile: String) {
        saveFirstNellSpFirstNellValue(SP_NARSCASH_KEY_NARSCASH_MOBILE, gMobile)
    }

    /** 获取 mobile */
    @SuppressLint("ApplySharedPref")
    fun getFirstNellMobile(): String {
        return getFirstNellSp().getString(SP_NARSCASH_KEY_NARSCASH_MOBILE, "")
            .toString()
    }

    /** 保存 country */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellCountry(gCountry: String) {
        saveFirstNellSpFirstNellValue(
            SP_NARSCASH_KEY_NARSCASH_COUNTRY,
            gCountry
        )
    }

    /** 获取 country */
    @SuppressLint("ApplySharedPref")
    fun getFirstNellCountry(): String {
        return getFirstNellSp().getString(SP_NARSCASH_KEY_NARSCASH_COUNTRY, "")
            .toString()
    }

    /** 保存 api path */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellApiFirstNellPath(gApiPath: String) {
        saveFirstNellSpFirstNellValue(
            SP_NARSCASH_KEY_NARSCASH_API_NARSCASH_PATH,
            gApiPath
        )
    }

    /** 获取 api path */
    @SuppressLint("ApplySharedPref")
    fun getFirstNellApiFirstNellPath(): String {
        return getFirstNellSp().getString(SP_NARSCASH_KEY_NARSCASH_API_NARSCASH_PATH, "")
            .toString()
    }

    /** 保存 ad source */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellAdFirstNellSource(gAdSource: String) {
        saveFirstNellSpFirstNellValue(
            SP_NARSCASH_KEY_NARSCASH_IS_NARSCASH_AD_NARSCASH_SOURCE,
            gAdSource
        )
    }

    /** 获取 ad source */
    @SuppressLint("ApplySharedPref")
    fun getFirstNellAdFirstNellSource(): String {
        return getFirstNellSp().getString(SP_NARSCASH_KEY_NARSCASH_IS_NARSCASH_AD_NARSCASH_SOURCE, "")
            .toString()
    }

    /** 保存 app review active */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellAppFirstNellReviewFirstNellActive(intValue: Int) {
        saveFirstNellSpFirstNellValueFirstNellInt(
            SP_NARSCASH_KEY_NARSCASH_APP_NARSCASH_REVIEW_NARSCASH_ACTIVE,
            intValue
        )
    }

    /** 获取 app review active */
    @SuppressLint("ApplySharedPref")
    fun getFirstNellAppFirstNellReviewFirstNellActive(): Int {
        return getFirstNellSp().getInt(SP_NARSCASH_KEY_NARSCASH_APP_NARSCASH_REVIEW_NARSCASH_ACTIVE, 0)
    }

    /** 保存 app review normal */
    @SuppressLint("ApplySharedPref")
    fun saveFirstNellAppFirstNellReviewFirstNellNormal(intValue: Int) {
        saveFirstNellSpFirstNellValueFirstNellInt(
            SP_NARSCASH_KEY_NARSCASH_APP_NARSCASH_REVIEW_NARSCASH_NORMAL,
            intValue
        )
    }

    /** 获取 app review normal */
    @SuppressLint("ApplySharedPref")
    fun getFirstNellAppFirstNellReviewFirstNellNormal(): Int {
        return getFirstNellSp().getInt(SP_NARSCASH_KEY_NARSCASH_APP_NARSCASH_REVIEW_NARSCASH_NORMAL, 0)
    }
}